package com.hernie.piggymall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hernie.common.utils.R;
import com.hernie.piggymall.ware.feign.MemberFeignService;
import com.hernie.piggymall.ware.vo.FareVo;
import com.hernie.piggymall.ware.vo.MemberAddressVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hernie.common.utils.PageUtils;
import com.hernie.common.utils.Query;

import com.hernie.piggymall.ware.dao.WareInfoDao;
import com.hernie.piggymall.ware.entity.WareInfoEntity;
import com.hernie.piggymall.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WareInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank((String) params.get("key"))) {
            queryWrapper.eq(WareInfoEntity::getId, params.get("key"))
                    .or().like(WareInfoEntity::getName, params.get("key"))
                    .or().like(WareInfoEntity::getAddress, params.get("key"))
                    .or().like(WareInfoEntity::getAreacode, params.get("key"));
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R r = memberFeignService.addrInfo(addrId);
        MemberAddressVo data = r.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>(){});
        if (data != null) {
            fareVo.setAddress(data);
            // TODO 获取运费
            //这里暂时固定
            BigDecimal fare = new BigDecimal(12);
            fareVo.setFare(fare);
            return fareVo;
        }
        return null;
    }

}